package com.xiaoyu.izf.controller.api;

import com.xiaoyu.izf.config.MyConfig;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.service.UserInfoService;
import com.xiaoyu.izf.util.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

@Controller
@RequestMapping("/api/qrcode")
public class QRCodeApiController extends BaseApiController{
    @Autowired
    private MyConfig myConfig;
    @Autowired
    private UserInfoService userInfoService;
    /**
     * 二维码图片
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/push_code")
    @ResponseBody
    public void pushCode(HttpServletRequest request, HttpServletResponse response,String pushCode)throws Exception{
        System.out.println(pushCode);
        UserInfo user = userInfoService.userInfoByPushCode(pushCode);
        System.out.println(user);
        if(user!=null&&user.getVipType()>0){
            String qrcode_url = myConfig.getHost() + "user/reg_page?pushCode=" + pushCode;
            OutputStream outStream = response.getOutputStream();
            QRCodeUtil.encode(qrcode_url, null, outStream, false);
            outStream.flush();
            outStream.close();
        }

    }
}
